/***************************************************************************
 *   oboe.controllers.jml.helpers.scrollBar.VBar                           *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.jml.helpers.scrollBar
{
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.events.Event;
	
	public class VBar extends Bar
	{
		public function VBar( container:DisplayObjectContainer )
		{
			super( container );
		}
		
		public override function update( dragging:Boolean=false ):void
		{
			this.container.y = (this.container.y + this.container.mask.y) - int(this.container.height * this.offset);
			this.container.mask.y = int(this.container.height * this.offset);
			
			this.graphics.clear();
			
			var vlimit:Number = this.container.mask.height - (this.isSmall?Bar.CSIZE:0);
			this.container.removeChild( this.container.mask );
			var ratio:Number = this.container.height / vlimit;
			this.container.addChild( this.container.mask );
			if( ratio <= 1 )
			{
				if( !this.isHidden )
				{
					this.emit("hideBar");
				}
				this.isHidden = true;
				return;
			}
			if( this.isHidden )
			{
				this.emit("showBar");
				this.isHidden = false;
			}

			this.graphics.lineStyle( 1, 0x5F5F5F, 1 );
			this.graphics.beginFill( this.barFillColor );
			this.graphics.drawRect( 0, 0, Bar.CSIZE-1, vlimit-1 );
			this.graphics.endFill();
			
			this.gradMat.createGradientBox( Bar.CSIZE, vlimit / ratio, 0 );
			this.graphics.lineStyle( 1, 0x5F5F5F, 1 );
			if( dragging )
			{
				this.graphics.beginGradientFill( GradientType.LINEAR, [this.btnDragColor1, this.btnDragColor2], [1, 1], [0, 255], this.gradMat );
			}
			else
			{
				this.graphics.beginGradientFill( GradientType.LINEAR, [this.btnFillColor1, this.btnFillColor2], [1, 1], [0, 255], this.gradMat );
			}
			this.graphics.drawRect( 0, vlimit * this.offset, Bar.CSIZE-1, vlimit/ratio-1 );
			this.graphics.endFill();
			
			this.y = this.container.y + this.container.mask.y;
			this.x = this.container.x + this.container.mask.x + this.container.mask.width - Bar.CSIZE;
		}
		
		protected override function onDrag():void
		{
			var vlimit:Number = this.container.mask.height - (this.isSmall?Bar.CSIZE:0);
			this.container.removeChild( this.container.mask );
			var ratio:Number = this.container.height / vlimit;
			this.container.addChild( this.container.mask );
			var btnSize:Number = vlimit/ratio;
			this.offset = (this.mouseY-btnSize/2) / vlimit;
			if( this.offset < 0 ) this.offset = 0;
			if( this.offset > 1-btnSize/vlimit ) this.offset = 1-btnSize/vlimit;
		}
		
	}
}